<?php
/**
 * prediction.php
 * 
 * Written by: A. Sari
 * $Id: prediction.php 21 2009-09-03 08:10:09Z ahmtsari $
 */

require_once("user.php");
require_once("game.php");

class Prediction {
	var $id;
	var $game_id;
	var $game_date;
	var $username;
	var $home_team;
	var $away_team;
	var $home_score;
	var $away_score;
	var $pts;
	
	function Prediction($id = 0, $u = NULL) {
		global $session;
		$this->id = $id;
		//get game & pred data
		if (isset($u))
			$this->username = $u;
		else
			$this->username = $session->username; 
	}
	
	function setPrediction($game_id) {
		global $database;
		$q = "SELECT * FROM predictions WHERE username='$this->username' AND game_id=$game_id";
		foreach ($database->cursor($q) as $row)	{
			$this->id = $row["id"];
			$this->home_score = $row["home_score"];
			$this->away_score = $row["away_score"];
			$this->pts = $row["pts"];
			break;
		}
	}
	
	function upsert($game_id) {
		global $database;
		
		//test if game is already started
		$game = new Game($game_id);
		if (strtotime($game->date) < strtotime(TIME_OFFSET))
			return;
		
		if (!isset($this->home_score) || $this->home_score == "")
			$home = "NULL";
		else
			$home = $this->home_score;
			
		if (!isset($this->away_score) || $this->away_score == "")
			$away = "NULL";
		else
			$away = $this->away_score;
					
		if (isset($this->id) && $this->id > 0) {
			if ($home != "NULL" && $away != "NULL") {	
				$q = "UPDATE predictions";
				$q .= " SET home_score = $home, away_score=$away";
			}
			else
				$q .= "DELETE FROM predictions";
			$q .= " WHERE id=$this->id;";
			return $database->query($q);
		}
		else if ($home != "NULL" && $away != "NULL") {
			$q = "INSERT INTO predictions (username, game_id, home_score, away_score)";
			$q .= " VALUES ('$this->username', $game_id, $home, $away);";
			return $database->query($q);
		}
				
	}	
	
	function updatePts() {
		global $database;
		$q = "UPDATE predictions SET pts = $this->pts";
		$q .= " WHERE id=$this->id;";
		return $database->query($q);
	}
	
	function userList($game_id = NULL) {
		global $database;
		$users = array();
		$q = "SELECT username, SUM(pts) as pts, COUNT(*) as cnt FROM predictions";
		if (isset($game_id))
			$q .= " WHERE game_id=$game_id";
		$q .= " GROUP BY username ORDER BY pts DESC";
		foreach ($database->cursor($q) as $row)	{
			array_push($users, new User($row["username"], $row["pts"], $row["cnt"]));
		}
		return $users;
	}	
	
	function getList($username = NULL, $game_id = NULL) {
		global $database;
		$preds = array();
		$q = "SELECT p.game_id, p.username, g.game_date,"
			. " t1.Team As home_team, t2.Team As away_team,"
			. " p.home_score, p.away_score, p.pts"
			. " FROM predictions p, teams t1, teams t2, games g"
			. " WHERE p.game_id = g.id"
			. " AND g.home_team_id = t1.id"
			. " AND g.away_team_id = t2.id";
			//. " AND (g.home_team_score IS NOT NULL OR g.home_team_score IS NOT NULL)";
		if (isset($username) && $username != "")
			$q .= " AND p.username='$username'";
		if (isset($game_id))
			$q .= " AND p.game_id=$game_id";
		$q .= " ORDER BY g.game_date DESC";
		foreach ($database->cursor($q) as $row)	{
			$p = new Prediction(0);
			$p->game_id = $row["game_id"];
			$p->username = $row["username"];
			$p->game_date = $row["game_date"];
			$p->home_team = $row["home_team"];
			$p->away_team = $row["away_team"];
			$p->home_score = $row["home_score"];
			$p->away_score = $row["away_score"];
			$p->pts = $row["pts"];
			array_push($preds, $p);
			unset($p);
		}
		return $preds;
	}		
}
?>